package LeetCode;

import java.util.*;

public class LC144 {
    public static void main(String[] args) {

    }
    public List<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> ans = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        if (root ==null) return ans;
        stack.add(root);
        while (!stack.isEmpty()){
            TreeNode pop = stack.pop();
            ans.add(pop.val);
            if(pop.right!=null){
                stack.add(pop.right);
            }
            if(pop.left!=null){
                stack.add(pop.left);
            }
        }
        return ans;
    }
}
